<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';

const router = useRouter();
const route = useRoute();
const isSidebarOpen = ref(true);
const currentUser = ref('');

// 侧边栏菜单
const menuItems = ref([
  {
    id: 'dashboard',
    title: '仪表盘',
    icon: 'fa-tachometer',
    route: '/admin/dashboard'
  },
  {
    id: 'users',
    title: '用户管理',
    icon: 'fa-users',
    route: '/admin/users'
  },
  {
    id: 'regions',
    title: '地区管理',
    icon: 'fa-map-marker',
    route: '/admin/regions'
  },
  {
    id: 'cities',
    title: '城市管理',
    icon: 'fa-building',
    route: '/admin/cities'
  },
  {
    id: 'spots',
    title: '景点管理',
    icon: 'fa-camera',
    route: '/admin/spots'
  },
  {
    id: 'food',
    title: '美食攻略',
    icon: 'fa-cutlery',
    route: '/admin/food'
  },
  {
    id: 'comments',
    title: '评论管理',
    icon: 'fa-comments',
    route: '/admin/comments'
  },
  {
    id: 'hobbies',
    title: '旅游偏好',
    icon: 'fa-heart',
    route: '/admin/hobbies'
  }
]);

onMounted(() => {
  // 检查是否已登录
  const isLoggedIn = localStorage.getItem('adminLoggedIn');
  if (!isLoggedIn) {
    router.push('/admin/login');
    return;
  }
  
  // 获取用户名
  currentUser.value = localStorage.getItem('adminUsername') || '管理员';
  
  // 如果没有子路径，默认跳转到仪表盘
  if (route.path === '/admin') {
    router.push('/admin/dashboard');
  }
});

const toggleSidebar = () => {
  isSidebarOpen.value = !isSidebarOpen.value;
};

const logout = () => {
  localStorage.removeItem('adminLoggedIn');
  localStorage.removeItem('adminUsername');
  router.push('/admin/login');
};
</script>

<template>
  <div class="min-h-screen bg-gray-50">
    <!-- 顶部导航栏 -->
    <header class="bg-white shadow">
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="flex justify-between h-16">
          <div class="flex items-center">
            <button 
              @click="toggleSidebar" 
              class="p-2 rounded-md text-gray-500 hover:text-gray-900 hover:bg-gray-100 focus:outline-none"
            >
              <i class="fa fa-bars"></i>
            </button>
            <h1 class="ml-4 text-xl font-bold text-gray-900">旅游社交平台 - 管理后台</h1>
          </div>
          <div class="flex items-center">
            <div class="ml-3 relative">
              <div class="flex items-center">
                <span class="text-gray-700 mr-2">{{ currentUser }}</span>
                <button 
                  @click="logout" 
                  class="p-1 rounded-full text-gray-500 hover:text-gray-700 hover:bg-gray-100 focus:outline-none"
                >
                  <i class="fa fa-sign-out"></i>
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </header>

    <div class="flex">
      <!-- 侧边栏 -->
      <aside 
        :class="[
          'bg-white shadow-md h-[calc(100vh-4rem)] fixed z-20 transition-all duration-300',
          isSidebarOpen ? 'w-64' : 'w-20'
        ]"
      >
        <nav class="p-4 space-y-1">
          <router-link 
            v-for="item in menuItems" 
            :key="item.id"
            :to="item.route"
            class="flex items-center p-2 rounded-md text-gray-600 hover:bg-gray-100 hover:text-indigo-600 group transition-all"
            active-class="bg-indigo-100 text-indigo-600 font-medium"
          >
            <i :class="['fa', item.icon, 'w-6 h-6 text-center']"></i>
            <span 
              :class="[
                'ml-3 text-sm font-medium',
                isSidebarOpen ? 'block' : 'hidden'
              ]"
            >
              {{ item.title }}
            </span>
          </router-link>
        </nav>
      </aside>

      <!-- 主内容区域 -->
      <main 
        :class="[
          'flex-1 transition-all duration-300 pt-4 pb-12 px-4 sm:px-6 lg:px-8',
          isSidebarOpen ? 'ml-64' : 'ml-20'
        ]"
      >
        <!-- 路由视图 -->
        <router-view />
      </main>
    </div>
  </div>
</template>

<style scoped>
/* 简化的样式定义，使用Tailwind的内置类 */
</style>